package com.ctsi.ssdc.web;

import java.net.URI;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.lang.Integer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ctsi.ssdc.service.LitemallGoodsProductService;
import com.ctsi.ssdc.domain.LitemallGoodsProduct;

import com.ctsi.ssdc.exception.BadRequestAlertException;
import com.ctsi.ssdc.model.PageResult;
import com.ctsi.ssdc.util.HeaderUtil;
import com.ctsi.ssdc.util.ResponseUtil;


/**
 * REST controller for managing LitemallGoodsProduct.
 *
 * @author ctsi-biyi-generator
 *
 */
@Api(value = "/api",tags = {"litemall-goods-product-controller"})
@RestController
@RequestMapping("/api")
public class LitemallGoodsProductController {

    private final Logger log = LoggerFactory.getLogger(LitemallGoodsProductController.class);

    private static final String ENTITY_NAME = "litemallGoodsProduct";

    private final LitemallGoodsProductService litemallGoodsProductService;

    public LitemallGoodsProductController(LitemallGoodsProductService litemallGoodsProductService) {
        this.litemallGoodsProductService = litemallGoodsProductService;
    }

    @InitBinder   
    public void initBinder(WebDataBinder binder) {   
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");   
        dateFormat.setLenient(true);   
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   
    } 
   
    /**
     * POST  /litemallGoodsProducts : Create a new litemallGoodsProduct.
     *
     * @param litemallGoodsProduct the litemallGoodsProduct to create
     * @return the ResponseEntity with status 201 (Created) and with body the new litemallGoodsProduct,
     * or with status 400 (Bad Request) if the litemallGoodsProduct has already an id
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "body",dataType = "LitemallGoodsProduct",name = "litemallGoodsProduct",value = "the litemallGoodsProduct to create")
    })
    @ApiOperation(value = "POST  /litemallGoodsProducts : create a new litemallGoodsProduct.",
        notes = "POST  /litemallGoodsProducts : create a new litemallGoodsProduct.",httpMethod = "POST")
    @PostMapping("/litemallGoodsProducts")
    public ResponseEntity<LitemallGoodsProduct> createLitemallGoodsProduct(@RequestBody LitemallGoodsProduct litemallGoodsProduct) throws URISyntaxException {
        log.debug("REST request to save LitemallGoodsProduct : {}", litemallGoodsProduct);
        if (litemallGoodsProduct.getId() != null) {
            throw new BadRequestAlertException("A new litemallGoodsProduct cannot already have an id", ENTITY_NAME, "idexists");
        }
        LitemallGoodsProduct result = litemallGoodsProductService.insert(litemallGoodsProduct);
        return ResponseEntity.created(new URI("/api/litemallGoodsProducts" + "/" +result.getId() ))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }
	
    /**
     * PUT  /litemallGoodsProducts : Updates an existing litemallGoodsProduct.
     *
     * @param litemallGoodsProduct the litemallGoodsProduct to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated litemallGoodsProduct,
     * or with status 400 (Bad Request) if the litemallGoodsProduct is not valid,
     * or with status 500 (Internal Server Error) if the litemallGoodsProduct couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "body",dataType = "LitemallGoodsProduct",
        name = "litemallGoodsProduct",value = "the litemallGoodsProduct to update")
    })
    @ApiOperation(value = "PUT  /litemallGoodsProducts : updates an existing litemallGoodsProduct.",
        notes = "PUT  /litemallGoodsProducts : updates an existing litemallGoodsProduct.",httpMethod = "PUT")
    @PutMapping("/litemallGoodsProducts")
    public ResponseEntity<LitemallGoodsProduct> updateLitemallGoodsProduct(@RequestBody LitemallGoodsProduct litemallGoodsProduct) throws URISyntaxException {
        log.debug("REST request to update LitemallGoodsProduct : {}", litemallGoodsProduct);
	    if (litemallGoodsProduct.getId() == null) {
	    	return createLitemallGoodsProduct(litemallGoodsProduct);
	    }
        LitemallGoodsProduct result = litemallGoodsProductService.update(litemallGoodsProduct);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * GET  /litemallGoodsProducts/:id : get the "id" litemallGoodsProduct.
     *
     * @param id the id of the litemallGoodsProduct to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the litemallGoodsProduct, or with status 404 (Not Found)
     */
//    @ApiImplicitParams({
//        @ApiImplicitParam(paramType = "path",dataType = "INTEGER",name = "id",
//        value = "the id of the litemallGoodsProduct to retrieve")
//    })
//    @ApiOperation(value = "GET  /litemallGoodsProducts/id : get the id litemallGoodsProduct.",
//        notes = "GET  /litemallGoodsProducts/id : get the id litemallGoodsProduct.",httpMethod = "GET")
//    @GetMapping("/litemallGoodsProducts/{id}")
//    public ResponseEntity<LitemallGoodsProduct> getLitemallGoodsProduct(@PathVariable Integer id) {
//        log.debug("REST request to get LitemallGoodsProduct : {}", id);
//        LitemallGoodsProduct litemallGoodsProduct = litemallGoodsProductService.findOne(id);
//        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(litemallGoodsProduct));
//    }

    /**
    * GET  /litemallGoodsProducts : get the litemallGoodsProducts firstStringBaseColumn.
    *
    * @return the ResponseEntity with status 200 (OK) and the list of litemallGoodsProducts in body
    */
    @ApiOperation(value = "GET  /litemallGoodsProducts : get the litemallGoodsProducts firstStringBaseColumn.",
                notes = "GET  /litemallGoodsProducts : get the litemallGoodsProducts firstStringBaseColumn.",httpMethod = "GET")
    @GetMapping("/litemallGoodsProducts")
    public PageResult<LitemallGoodsProduct> getLitemallGoodsProductsList(String specifications ,Pageable pageable) {
        log.debug("REST request to get LitemallGoodsProductsList");
        return litemallGoodsProductService.findFirstStringColumn(specifications ,pageable);
    }
	
    /**
     * DELETE  /litemallGoodsProducts/:id : delete the "id" litemallGoodsProduct.
     *
     * @param id the id of the litemallGoodsProduct to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path",dataType = "INTEGER",
            name = "id",value = "the id of the litemallGoodsProduct to delete")
    })
    @ApiOperation(value = "DELETE  /litemallGoodsProducts/id : delete the id litemallGoodsProduct.",
        notes = "DELETE  /litemallGoodsProducts/id : delete the id litemallGoodsProduct.",httpMethod = "DELETE")
    @DeleteMapping("/litemallGoodsProducts/{id}")
    public ResponseEntity<Void> deleteLitemallGoodsProduct(@PathVariable Integer id) {
        log.debug("REST request to delete LitemallGoodsProduct : {}", id);
        litemallGoodsProductService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
    
}
